﻿using System;
using System.Text;
using System.Web;

public class CookieHelper
{
    public static void setCookie(string key, string value, TimeSpan expires)
    {
        HttpCookie cookie = new HttpCookie(key);
        cookie.Value = StringHelper.UrlEncrypt(value);
        cookie.Expires = DateTime.Now.Add(expires);

        if (HttpContext.Current.Request.Cookies[key] == null)
        {
            HttpContext.Current.Response.Cookies.Add(cookie);
        }
        else
        {
            HttpContext.Current.Response.Cookies.Set(cookie);
        }
    }

    public static string getCookie(string key)
    {
        if (HttpContext.Current.Request.Cookies[key] != null)
        {
            HttpCookie c = HttpContext.Current.Request.Cookies[key];
            if (!string.IsNullOrEmpty(c.Value))
            {
                return StringHelper.UrlDecrypt(HttpContext.Current.Request.Cookies[key].Value);
            }
            else
            {
                return string.Empty;
            }
        }
        else
        {
            return string.Empty;
        }
    }

    public static void removeCookie(string key)
    {
        if (HttpContext.Current.Request.Cookies[key] != null)
        {
            HttpCookie cookie = HttpContext.Current.Request.Cookies[key];
            cookie.Expires = DateTime.Now.AddMonths(-1);
            cookie.Value = string.Empty;

            HttpContext.Current.Response.Cookies.Set(cookie);
        }
    }
}
